package com.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hospital.configure.Result;
import com.hospital.entity.MedicineType;
import com.hospital.entity.MedicineType;
import com.hospital.mapper.MedicineTypeMapper;
import com.hospital.mapper.MedicineTypeMapper;
import com.hospital.service.MedicineTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WuLiting
 * @since 2021-04-10
 */
@Service
public class MedicineTypeServiceImpl extends ServiceImpl<MedicineTypeMapper, MedicineType> implements MedicineTypeService {


    @Resource
    MedicineTypeMapper medicineTypeMapper;

    @Override
    public Result<MedicineType> updateMedicineType(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();
        try {
            if (StringUtils.isEmpty(medicineType.getMedicineTypeId())) {
                //如果ID为空，代表添加
                medicineType.setMedicineTypeId(IDGenerator.getUUID("MEDICINETYPE"));
                medicineTypeMapper.insert(medicineType);
            } else {
                //如果ID不为空，代表修改
                medicineTypeMapper.updateById(medicineType);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MedicineType> delMedicineType(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();
        try {
            if (StringUtils.isEmpty(medicineType.getMedicineTypeId())) {
                result.setMessage("medicineTypeId can't be empty");
                return result;
            }
            medicineTypeMapper.deleteById(medicineType.getMedicineTypeId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MedicineType> batchDelMedicineType(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(medicineType.getIds())) {
                String[] ids = medicineType.getIds().split(",");
                for (String id : ids) {
                    medicineTypeMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MedicineType> queryMedicineType(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();
        try {
            QueryWrapper<MedicineType> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(medicineType.getMedicineTypeName())) {
                wrapper.lambda().eq(MedicineType::getMedicineTypeName, medicineType.getMedicineTypeName());
            }

            List<MedicineType> medicineTypes = medicineTypeMapper.selectList(wrapper);
            result.setReturnList(medicineTypes);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MedicineType> queryMedicineTypeByPage(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();

        try {
            if (medicineType.getCurrent() == null || medicineType.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<MedicineType> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            IPage<MedicineType> medicineTypes = medicineTypeMapper.selectPage(new Page<>(medicineType.getCurrent(), medicineType.getSize()), wrapper);
            result.setReturnPage(medicineTypes);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<MedicineType> queryMedicineTypeById(MedicineType medicineType) {
        Result<MedicineType> result = new Result<>();
        try {
            MedicineType returnMedicineType = medicineTypeMapper.selectById(medicineType.getMedicineTypeId());
            result.setReturnObj(returnMedicineType);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
